/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vinisoft.gymwebapp.model.facades;

import com.vinisoft.gymwebapp.model.entities.GymMenu;
import com.vinisoft.gymwebapp.model.entities.GymRol;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;

/**
 *
 * @author vinikike
 */
@Stateless
@LocalBean
public class GymMenuFacade extends DefaultAbstractFacade<GymMenu, Integer> {

    public GymMenuFacade() {
        super(GymMenu.class);
    }

    public List<GymMenu> findAllNivel0(GymRol rol) {
        TypedQuery<GymMenu> q = getEntityManager().createQuery("SELECT m FROM GymMenu m JOIN m.gymRolList mr where m.menuPadre IS NULL AND mr.id = :rol ORDER BY m.id ASC", GymMenu.class);
        q.setParameter("rol", rol.getId());
        return q.getResultList();
    }

    public List<Integer> menusHijosPorRol(GymMenu menuPadre, GymRol rol) {
        TypedQuery<Integer> q = getEntityManager().createQuery("SELECT m.id FROM GymMenu m JOIN m.gymRolList mr where m.menuPadre = :menuPadre AND mr.id = :rol ORDER BY m.id ASC", Integer.class);
        q.setParameter("menuPadre", menuPadre);
        q.setParameter("rol", rol.getId());
        return q.getResultList();
    }

}